<template>
  <van-popup
    :value="value"
    position="top"
    :style="{ height: '100%' }"
    closeable
    @click-close-icon="$emit('input', false)"
  >
    <div class="article-channel">
      <div class="head">
        <h3>我的频道<small>点击进入频道</small></h3>
        <a class="edit" href="javascript:;">编辑</a>
      </div>
      <div class="body">
        <a href="javascript:;" @click="enterChannel(index)" v-for="(item,index) in MyChannels" :key="item.id" :class="{active:activeIndex === index}">{{ item.name }}</a>
      </div>
      <div class="head" style="margin-top: 12px">
        <h3>频道推荐</h3>
      </div>
      <div class="body">
        <a href="javascript:;" v-for="item in optionalChannels" :key="item.id">{{ item.name }}</a>
      </div>
    </div>
  </van-popup>
</template>

<script>
// 频道接口
import { getAllChannels } from '@/api/channel'
export default {
  name: '',
  props: {
    // 控制频道组件的显示和隐藏
    value: {
      type: Boolean,
      default: false
    },
    // 渲染频道
    MyChannels: {
      type: Array,
      default: () => []
    },
    // 当前频道激活组件的索引
    activeIndex: {
      type: Number,
      default: 0

    }
  },
  async  created () {
    // 获取全部频道数据
    const [, res] = await getAllChannels()

    // 赋值给全部频道
    this.allChannels = res.data.data.channels
  },
  computed: {
    // 可选频道(频道推荐)
    optionalChannels () {
      // 遍历所有频道，根据频道ID去我的频道查找，如果有就排除，如果没有找到就返回
      return this.allChannels.filter(item => !this.MyChannels.find(mc => mc.id === item.id))
    }
  },
  data () {
    return {
      // 全部频道
      allChannels: []
    }
  },
  methods: {
    // 点击进入频道
    enterChannel (index) {
      // 俩个功能需求
      // 1.点击关闭对话框
      this.$emit('input', false)
      // 2.传递当前点击频道索引给首页组件
      this.$emit('update:activeIndex', index)
    }
  }
}
</script>

<style scoped lang="less">
.van-popup {
  width: 100%;
  height: 100%;
  ::v-deep .van-popup__close-icon {
    font-size: 20px;
    right: 12px;
    top: 12px;
  }
}
// 频道样式

.article-channel {
  margin-top: 44px;
  .head {
    padding: 0 16px;
    display: flex;
    justify-content: space-between;
    justify-items: center;
    padding-bottom: 12px;
    h3 {
      font-size: 16px;
      color: #333;
      margin: 0;
      small {
        font-size: 12px;
        color: #999;
        margin-left: 10px;
      }
    }
    .edit {
      float: right;
      height: 22px;
      width: 52px;
      line-height: 22px;
      text-align: center;
      color: #DE644B;
      border-radius: 11px;
      border: 1px solid #DE644B;
      font-size: 12px;
      &.active {
        color: #fff;
        background: #DE644B;
      }
    }
  }
  .body {
    padding: 0 6px 0 16px;
    a {
      display: inline-block;
      padding: 0 8px;
      font-size:14px;
      color: #3A3948;
      background: #F7F8FA;
      height: 36px;
      line-height: 36px;
      min-width: 78px;
      margin-right: 10px;
      margin-bottom: 12px;
      border-radius: 18px;
      text-align: center;
      &.active {
        color: @geek-color;
      }
    }
  }
}
</style>
